package top.ruanlitao.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.ruanlitao.shortlink.project.dao.entity.LinkBrowserStatsDO;
import top.ruanlitao.shortlink.project.dto.req.ShortLinkStatsReqDTO;

import java.util.HashMap;
import java.util.List;

/**
 * 浏览器统计访问监控持久层
 */
public interface LinkBrowserStatsMapper extends BaseMapper<LinkBrowserStatsDO> {

    /**
     * 记录浏览器访问监控数据
     */
    @Insert("""
        INSERT INTO t_link_browser_stats (full_short_url, date, cnt, browser, create_time, update_time, del_flag)
        VALUES( #{linkBrowserStats.fullShortUrl}, #{linkBrowserStats.date}, #{linkBrowserStats.cnt}, #{linkBrowserStats.browser}, NOW(), NOW(), 0)
        ON DUPLICATE KEY UPDATE cnt = cnt +  #{linkBrowserStats.cnt};
    """)
    void shortLinkBrowserState(@Param("linkBrowserStats") LinkBrowserStatsDO linkBrowserStatsDO);

    /**
     * 根据短链接获取指定日期内浏览器监控数据
     */
    @Select("""
        SELECT
            tlbs.browser,
            SUM(tlbs.cnt) AS count
        FROM
            t_link tl INNER JOIN
            t_link_browser_stats tlbs ON tl.full_short_url = tlbs.full_short_url
        WHERE
            tlbs.full_short_url = #{param.fullShortUrl}
            AND tl.gid = #{param.gid}
            AND tl.del_flag = '0'
            AND tl.enable_status = #{param.enableStatus}
            AND tlbs.date BETWEEN #{param.startDate} AND #{param.endDate}
        GROUP BY
            tlbs.full_short_url, tl.gid, tlbs.browser
    """)
    List<HashMap<String, Object>> listBrowserStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);
}
